<?php
include_once ('struct_Version.php');
include_once ('struct_Main.php');
include_once ('struct_error.php');

//配置从配置文件获取
$GLOBALS['licenseServer']=$GLOBALS['context']->get_app_conf('licenseServer');//"192.168.170.19";
$GLOBALS['LicensePort']=$GLOBALS['context']->get_app_conf('LicensePort');//4018 ;

class Socket {

	var $time_limit = 0;

	var $socket = false;

	var $MsgVersion = 0;

	var $MsgType = 0;

	var $MsgKey = '';

	var $MsgLength = 0;

	var $MsgXor = '';

	var $ZTCompany = '';
	function __construct() {
		$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if ($this->socket === false) {
			return  socket_strerror(socket_last_error()) . "\n";
		}else{
                    //$com    = $GLOBALS['db']->getOne(' select co_name from coinfo');
                    $com   ='baison';
		                $comany = iconv("utf-8", "gbk//ignore", $com);
		                $this ->ZTCompany = $comany;

		}

	}

	//创建
	function Socket_Build() {
		if ($this->socket === false) {
			return  array('code'=>0,'msg'=>'加密服务创建失败');
			//return false;
		} else {
			$result = @socket_connect($this->socket,$GLOBALS['licenseServer'], $GLOBALS['LicensePort']);
			if ($result === false) {
				$msg = '连接失败！';
				$url = 'privilege.php?act=login';
				//return  socket_strerror(socket_last_error($this->socket)) . "\n";
				//jm_alert($msg,$url);
				return  array('code'=>0,'msg'=>'加密服务器连接失败');
			} else {
				return  array('code'=>1,'msg'=>'连接成功');
			}
		}
	}

	//发送数据
	/*
	*	totle : 当前总用户数
	*	chg_num:需要变动的用户数
	*/
	//发送验证信息
	function Socket_Yanzhen($opt = 0) {
		$send_info = new send_info();
		$send_info->MsgVersion = 3000;
		$send_info->MsgType = $opt;
		$send_info->MsgKey = 2000;
		$send_info->MsgLength = 4000;
		$send_info->MsgXor = 1;
		$msg = pack("i*", $send_info->MsgVersion);
		$msg .= pack("i*", $send_info->MsgType);
		$msg .= pack("i*", $send_info->MsgKey);
		$msg .= pack("i*", $send_info->MsgLength);
		$msg .= pack("c*", $send_info->MsgXor);
		@socket_write($this->socket, $msg, 17);

	}
	//发送验证信息
	function Socket_Main($iModle=0,$iPreCount=1,$opt,$SHardInfo=1,$dm,$mc) {
		$main_info = new Main_info();
		$main_info->MsgType = $opt;
		$main_info->iProduct = 200;
		$main_info->iModle = $iModle;
		$main_info->vToSrvMsgInfo = 1;
		$main_info->ZTCompany = substr($this->ZTCompany,0,100);//传输公司名称
		$main_info->ZTName = $GLOBALS['ZTName'];
		$main_info->ZTDBName = $GLOBALS['ZTDBName'];
		//$main_info->iPreCount = $iPreCount;
                $main_info->iPreCount = 0;
		$main_info->lRtnCode = 1;
		$main_info->sLoginDM =substr($dm,0,30);
		$main_info->SHardInfo = $SHardInfo;
		$main_info->SLoginMC = 1;
		$main_info->SType = substr($mc,0,50);
		$main_info->MsgXor = 1;

		$msg = pack("i*", $main_info->MsgType);
		$msg .= pack("i*", $main_info->iProduct);
		$msg .= pack("c*", $main_info->iModle);
		$msg1 = pack("a100", $main_info->ZTCompany);
		$msg1 .= pack("a100", $main_info->ZTName);
		$msg1 .= pack("a50", $main_info->ZTDBName);
		$msg1 .= pack("i*", $main_info->iPreCount);
		$msg1 .= pack("a20", $main_info->lRtnCode);
		$msg1 .= pack("a30", $main_info->sLoginDM);
		$msg1 .= pack("a50", $main_info->SLoginMC);
		$msg1 .= pack("a50", $main_info->SHardInfo);
		$msg1 .= pack("c*", $main_info->SType);
		$main_info->MsgXor=xor_enc_Stream($msg1);
                $msg .= pack("a405", $msg1);
		$msg .= pack("c*", $main_info->MsgXor);
		socket_write($this->socket, $msg,415);
	}


	//发送验证信息
	function Socket_DecyptPwd($lRtnhanld,$pwd) {
		$decyptpwd = new DecyptPwd_info();
		$decyptpwd->MsgType = 4000;
		$decyptpwd->iProduct = 200;
		$decyptpwd->iModle = 0;
		$decyptpwd->lRtnCode=$lRtnhanld;
		$decyptpwd->Msginfo  = substr($pwd,0,100);
		$decyptpwd->MsgXor = 1;

		$msg = pack("i*", $decyptpwd->MsgType);
		$msg .= pack("i*", $decyptpwd->iProduct);
		$msg .= pack("c*", $decyptpwd->iModle);
		$msg .= pack("a20", $decyptpwd->lRtnCode);
		$msg1= pack("a100", $decyptpwd->Msginfo);
		$decyptpwd->MsgXor=xor_enc_Stream($msg1);
    $msg .= pack("a100", $msg1);
		$msg .= pack("c*", $decyptpwd->MsgXor);
		socket_write($this->socket, $msg,130);
	}

	//关闭
	function Socket_Cls() {
		socket_clear_error($this->socket);
		socket_close($this->socket);
	}

	//接收第一次通讯返回过来的值
	function get_rev_info($close_flag = 0,&$rtnMsg) {
	$result_Ency1 ='';
	if ($strRecvData = @socket_read($this->socket, 537)) {
		$format = "imsgVersion/imsgType/imsgKey/C524msgRtninfo/CbXOR";
		$result = unpack("$format", $strRecvData);
		$str_encry=array();
    for($i=1;$i<525;$i++){
    	$str_encry[$i]=$result['msgRtninfo'.$i]; }
		$bXor =xor_enc($str_encry);
		if ($bXor==$result['bXOR']) {
			$format1="a20msgRtninfo/iLErrCode/a500LErrmsg";
			$str_Decrypt=Decrypt_str($str_encry,2000);
			for($i=1;$i<525;$i++){
    	  $result_Ency1.=pack("C*",$str_Decrypt[$i]);
      }
			$result_Ency=unpack("$format1",$result_Ency1 );
			if($result_Ency['LErrCode'] == 900000)
				{
					$rtnMsg=$result_Ency['LErrmsg'];
					return $result_Ency['LErrCode'];
					if($close_flag)//按要求关闭
					{
						$this->Socket_Cls();
					}
				}else{
					return $result_Ency['LErrCode'];
					$this->Socket_Cls();
				}

				//正确进行下一步操作
			} else {
				return '210001';
				$sErrMsg="";
				$this->Socket_Cls();
			}

		}else{
			return '3000001';
			$sErrMsg="";
			$this->Socket_Cls();
		}


	}
}

function zdsh($now_status,$opt)
{
	//return ;
	$socket = new Socket();
	$socket->Socket_Build();
	$socket->Socket_Yanzhen($opt);
	$rev_info = $socket->get_rev_info();
	$result= array();
	$msg='';
    if($rev_info == '900000')
    {
    	$count_zd_users = $socket->get_now_shop_user();
    	$socket->Socket_Main(1,$count_zd_users,$opt);
    	$rev_info2 = $socket->get_rev_info();

    	if($rev_info2 != '900000')
    	{
    		$error_info = error_list();
    		$now_status = $rev_info2;
    		$msg = $error_info[$rev_info2]['msg'];
    	}else{
			$now_status = $now_status;
		}
   }else{
    		//返回错误信息，弹出警告
    		$error_info = error_list();
    		$now_status = $rev_info;
    		$msg = $error_info[$rev_info]['msg'];
    }
    $socket->Socket_Cls();
	$result['status'] = $now_status;
	$result['msg'] = $msg;
	return $result;

}

function orgyh($opt)
{
	//return ;
	    $socket = new Socket();
    	$socket->Socket_Build();
		$socket->Socket_Yanzhen($opt);
		$rev_info = $socket->get_rev_info();
    	if($rev_info == '900000')
    	{
    		//$count_manage_users = $socket->get_now_manage_user();//后台用户
                $count_manage_users =1;
    		$socket->Socket_Main(0,$count_manage_users,$opt);
    		$rev_info2 = $socket->get_rev_info();

    		if($rev_info2 != '900000')
    		{
    			$error_info = error_list();
    			$socket->Socket_Cls();
    			//@make_json_alert($error_info[$rev_info2]['msg'],'pop','privilege.php?act=login');
				exit;
    		}
    		$socket->Socket_Cls();
    	}else{
    		//返回错误信息，弹出警告
    		$error_info = error_list();
    		$socket->Socket_Cls();
    		//@make_json_alert($error_info[$rev_info]['msg'],'pop','privilege.php?act=login');
			exit;
    	}

}
function zdyh($opt,$SHardInfo=1)
{
	//return ;
		$socket = new Socket();
    $socket->Socket_Build();
		$socket->Socket_Yanzhen($opt);
		$sRtnMsg="";
		$rev_info = $socket->get_rev_info(0,$sRtnMsg);
    	if($rev_info == '900000')
    	{
    		$count_zd_users = $socket->get_now_shop_user();
    		$socket->Socket_Main(1,$count_zd_users,$opt,$SHardInfo);
    		$rev_info2 = $socket->get_rev_info();
    		if($rev_info2 != '900000')
    		{
    			$error_info = error_list();
    			$socket->Socket_Cls();
    			//make_json_alert($error_info[$rev_info2]['msg'],'pop','privilege.php?act=login');
				exit;
    		}
    		$socket->Socket_Cls();
    	}else{
    		//返回错误信息，弹出警告
    		$error_info = error_list();
    		$socket->Socket_Cls();
    		//make_json_alert($error_info[$rev_info]['msg'],'pop','privilege.php?act=login');
			exit;
    }

}

function Decrypt_pwd($SHardInfo=1,$Pwd,&$sRtnMsg)
{
		$sRtnMsg="";
	  $tmprtn=9;
		$socket = new Socket();
    	$build_data=$socket->Socket_Build();
    	if($build_data['code']==0){
    		return array('code'=>$tmprtn,'msg'=>$build_data['msg']);
    	}
		$socket->Socket_Yanzhen(4000);
		$rev_info = $socket->get_rev_info(1,$sRtnMsg);
    	if($rev_info == '900000')
    	{
    		$socket->Socket_DecyptPwd(1,$Pwd);

    		$rev_info2 = $socket->get_rev_info(1,$sRtnMsg);

    		if($rev_info2 != '900000')
    		{
    			$error_info = error_list();
    			$socket->Socket_Cls();
    			//make_json_alert($error_info[$rev_info2]['msg'],'pop','privilege.php?act=login');
    			$sRtnMsg = '';
				  //exit;
    		}else
    		{
    			  $tmprtn=0;
    		}
    		$socket->Socket_Cls();
    	}else{
    		//返回错误信息，弹出警告
    		$error_info = error_list();
    		$socket->Socket_Cls();
    		 $sRtnMsg ='密码服务连接失败！';
    		//make_json_alert($error_info[$rev_info]['msg'],'pop','privilege.php?act=login');
    		//echo "错误！";
			  //exit;
    }
    return array('code'=>$tmprtn,'msg'=>$sRtnMsg);

}

//异或
	function xor_enc($crytxt) {
	   //$crytxt = unpack("c*",$str);
	   if (count($crytxt)<=1) {
		  $b1=$crytxt[1];}
	   else{
		 $b1=$crytxt[1];
		 for ($i = 2; $i <= count($crytxt); $i++) {
			$b1 = $crytxt[$i] ^ $b1;
		   }
		}
	   return $b1;
	}

	function xor_enc_Stream($str) {
	   $crytxt = unpack("c*",$str);
	   if (count($crytxt)<=1) {
		  $b1=$crytxt[1];}
	   else{
		 $b1=$crytxt[1];
		 for ($i = 2; $i <= count($crytxt); $i++) {
			$b1 = $crytxt[$i] ^ $b1;
		   }
		}
	   return $b1;
	}


	function Decrypt_str($crytxt,$key){
		if (count($crytxt)>1) {
	   $tmpkey=$key;
		 $b1=$crytxt[1];
		 for ($i = 1; $i <= count($crytxt); $i++) {
		 	$b1=$crytxt[$i];
		 	if ($b1<11) {$b1=256+$b1;}
		 	$b1=($b1-11)^$tmpkey;
		  if ($b1>255){
		  	$b1=$b1 % 256;}
		 	$tmpkey=$crytxt[$i]+$tmpkey;
		 	if ($tmpkey>65535) { $tmpkey=$tmpkey % 65536;}
		 	$crytxt[$i]=$b1;
		  }
		}

	   return $crytxt;



	}


//	/*
//	调用
//	*/
//	$Decry_info="";
//  if (Decrypt_pwd(1,'sherp',$Decry_info)==0){
//  	echo "密码加密成功：".$Decry_info;
//  }
//  else
//  {
//  	echo "密码加密失败：";
//  }

?>